package com.astrob.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.astrob.NavFrameSDK;
import com.astrob.NavFrameSDK.NavDest;
import com.astrob.NavFrameSDK.POIDescription;
import com.astrob.adapters.ListA2VAdapter;
import com.astrob.model.FavoriteData;
import com.astrob.model.FavoriteFileHandle;
import com.astrob.model.HistoryData;
import com.astrob.model.Msg;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.naviframe.Start;
import com.astrob.util.Utils;
import com.astrob.nhcmap.R;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class PoiInfoActivity extends Activity {

	private ListView mListView;
	private TextView mPoiName;
	private TextView mPoiAddr;
	private TextView mPoiPhone;

	private Button mAddfav;
	private Button mAddfav2;

	private ListA2VAdapter mAdapter = null;
	private ArrayList<String> mListText = new ArrayList<String>();
	private ArrayList<String> mListText2 = new ArrayList<String>();

	private POIDescription mPoiData = NavFrameSDK.getInstance().new POIDescription();
	private int mTipIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_poiinfo);

		onInitUI();

		Bundle bundle = getIntent().getExtras();
		String tipname = "";
		if (bundle != null) {
			tipname = bundle.getString("tipname");
			if (tipname == null) {
				tipname = "";
			}
		}

		mTipIndex = -1;
		int size = Start.getInstance().MapKit().mTipPoiList.size();
		for (int i = 0; i < size; i++) {
			String name = Start.getInstance().MapKit().mTipPoiList.get(i).name;
			String spNames[] = name.split("\n");
			mListText.add(spNames[0]);
			if(spNames.length > 1) {
				mListText2.add(spNames[1]);
			}
			else {
				mListText2.add("");
			}
			if (name.contains(tipname) && mTipIndex < 0) {
				mTipIndex = i;
			}
		}

		if (mTipIndex < 0) {
			mTipIndex = 0;
			String spNames[] = tipname.split("\n");
			mListText.add(0, spNames[0]);
			if(spNames.length > 1) {
				mListText2.add(0, spNames[1]);
			}
			else {
				mListText2.add(0, "");
			}
			POIDescription poiDesription = NavFrameSDK.getInstance().new POIDescription();
			poiDesription.name = tipname;
			poiDesription.position.lon = Start.getInstance().MapKit().getMapCenter()
					.GetLon();
			poiDesription.position.lat = Start.getInstance().MapKit().getMapCenter()
					.GetLat();
			Start.getInstance().MapKit().mTipPoiList.add(0, poiDesription);
			mPoiData = poiDesription;
		}
		mAdapter = new ListA2VAdapter(this, mListText, mListText2);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				if (Utils.isFastDoubleClick())
					return;
				showPosInfo(position);
			}
		});

		showPosInfo(mTipIndex);

	}

	private void onInitUI() {
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(R.string.poiinfo_title_text);

		mPoiName = (TextView) findViewById(R.id.poiinfo_item_name);
		mPoiAddr = (TextView) findViewById(R.id.poiinfo_item_addr);
		mPoiPhone = (TextView) findViewById(R.id.poiinfo_item_phone);
		mListView = (ListView) findViewById(R.id.listview);
		mListView.setVerticalScrollBarEnabled(false);

		mAddfav = (Button) findViewById(R.id.btn_add_fav);
		mAddfav2 = (Button) findViewById(R.id.btn_add_fav2);
	}

	private void showPosInfo(int index) {
		if (Start.getInstance().MapKit().mTipPoiList == null)
			return;
		int size = Start.getInstance().MapKit().mTipPoiList.size();
		if (size < 1)
			return;
		if (index < 0 || index >= size) {
			mTipIndex = 0;
			index = 0;
		}

		mPoiData = Start.getInstance().MapKit().mTipPoiList.get(index);
		mPoiName.setText(mPoiData.name);
		mPoiAddr.setText(mPoiData.address);
		String tel = mPoiData.tel.replace(";", "\n");
		mPoiPhone.setText(tel);

		checkIsfav();
	}

	public void onClickedSearchArround(View v) {
		if (Utils.isFastDoubleClick())
			return;

		// 返回到上级界面防止循环创建
		setResult(Msg.MAP_SEARCHARROUNT);
		finish();
	}

	public void onAddRoute(View v) {
		if (Utils.isFastDoubleClick())
			return;
		HistoryData data = new HistoryData();
		data.name = mPoiData.name;
		data.lon = mPoiData.position.lon;
		data.lat = mPoiData.position.lat;
		data.address = mPoiData.address;

		RoutePlanDataHandle.getInstance().addDest(data);
		RoutePlanActivity.launch(this, 3);
		// Intent intent = new Intent(this, RouteManagerActivity.class);
		// // intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); // 防止创建多次
		// intent.putExtra("addDest", data);
		// startActivityForResult(intent, 3);
	}

	public void onClickedToNavi(View v) {
		if (Utils.isFastDoubleClick())
			return;
		Start.getInstance().MapKit().mNavDest = new NavDest[1];
		Start.getInstance().MapKit().mNavDest[0] = NavFrameSDK.getInstance().new NavDest();
		Start.getInstance().MapKit().mNavDest[0].pos.lon = mPoiData.position.lon;
		Start.getInstance().MapKit().mNavDest[0].pos.lat = mPoiData.position.lat;
		Start.getInstance().MapKit().mNavDest[0].name = mPoiData.name;
		Start.getInstance().MapKit().mNavDest[0].eid = 0;

		Start.getInstance().MapKit().clearNavStart();
		Start.getInstance().MapKit().setCarRouteOption();
		setResult(Msg.NAVI_TO_DEST);
		finish();
	}

	public void onAddfav(View v) {
		if (Utils.isFastDoubleClick())
			return;
		FavoriteData data = getFavData();
		FavoriteFileHandle.getInstance().add(data);
		Toast.makeText(this, getString(R.string.fav_collectsuccess),
				Toast.LENGTH_SHORT).show();

		mAddfav.setVisibility(View.GONE);
		mAddfav2.setVisibility(View.VISIBLE);
	}

	public void onDelfav(View v) {
		if (Utils.isFastDoubleClick())
			return;
		FavoriteData data = getFavData();

		FavoriteFileHandle.getInstance().delete(data);
		Toast.makeText(this, getString(R.string.fav_uncollect),
				Toast.LENGTH_SHORT).show();

		mAddfav2.setVisibility(View.GONE);
		mAddfav.setVisibility(View.VISIBLE);
	}

	private void checkIsfav() {
		FavoriteData data = getFavData();
		if (FavoriteFileHandle.getInstance().haveSameFav(data)) {
			mAddfav.setVisibility(View.GONE);
			mAddfav2.setVisibility(View.VISIBLE);
		} else {
			mAddfav2.setVisibility(View.GONE);
			mAddfav.setVisibility(View.VISIBLE);
		}
	}

	private FavoriteData getFavData() {
		FavoriteData data = new FavoriteData();
		data.name = mPoiData.name;
		data.lon = mPoiData.position.lon;
		data.lat = mPoiData.position.lat;
		data.address = mPoiData.address;
		data.phoneNum = mPoiData.tel;
		data.countryID = "CN";
		try {
			data.pcode = new String(mPoiData.pcode, 0, 4, "gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return data;
	}

	public void onBack(View v) {
		setResult(Msg.BACK_MAP_ID);
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			setResult(Msg.BACK_MAP_ID);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Msg.NAVI_TO_DEST) {
			setResult(Msg.NAVI_TO_DEST);
			finish();
			return;
		}
		checkIsfav();

		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
}
